package org.framework.edge.builder.docker;

import org.springframework.stereotype.Component;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.web.response.Result;

import java.util.Arrays;
import java.util.stream.Collectors;


@Component
public class DefaultDockerRuntime extends AbstractDockerRuntime {


    /**
     * 登录仓库
     *
     * @param url      登录地址
     * @param username 登录账号
     * @param password 登录密码
     * @return
     */
    @Override
    public Result<?> loginRepository(String url, String username, String password) {
        //            docker login docker-registry.com -u admin -p Harbor12345
        return doProcess("docker","login", url, "-u", username, "-p", password);
    }

    /**
     * tag
     *
     * @param imageOldName 旧的镜像
     * @param imageNewName 新的镜像
     * @return
     */
    @Override
    public Result<?> tag(String imageOldName, String imageNewName) {
        //        docker tag  minio/minio:RELEASE.2024-07-15T19-02-30Z 192.168.101.177:8080/shuhan/minio:RELEASE.2024-07-15T19-02-30Z
        return doProcess("docker","tag", imageOldName, imageNewName);

    }

    /**
     * 拉取镜像
     *
     * @param imageName 镜像名称
     * @return
     */
    @Override
    public Result<?> pull(String imageName) {
        return doProcess("docker","pull", imageName);
    }

    /**
     * 推送镜像
     *
     * @param imageName 镜像名称
     * @return
     */
    @Override
    public Result<?> push(String imageName) {
        return doProcess("docker","push", imageName);
    }

    /**
     * 删除镜像
     *
     * @param imageName 镜像名称
     * @return
     */
    @Override
    public Result<?> rmi(String... imageNames) {
        return doProcess("docker","rmi", Arrays.stream(imageNames).sequential().collect(Collectors.joining(NormalUsedString.EMPTY)));
    }
}
